package com.baosight.xdata.zyl;

import com.baosight.xdata.redis.LettuceUtil;
import io.lettuce.core.RedisURI;

import java.time.Duration;
import java.util.*;


public class TssRedisStreamProducer {
    public static String STREAM_1 = "zyl-stream:1";
    public static String STREAM_2 = "zyl-stream:2";

    private void initRedis() {
        Set<RedisURI> uris = new HashSet<>();
        uris.add(RedisURI.builder().withHost("10.25.10.102").withPort(6378).withTimeout(Duration.ofSeconds(30)).build());
        uris.add(RedisURI.builder().withHost("10.25.10.102").withPort(6377).withTimeout(Duration.ofSeconds(30)).build());
        uris.add(RedisURI.builder().withHost("10.25.10.102").withPort(6379).withTimeout(Duration.ofSeconds(30)).build());
        uris.add(RedisURI.builder().withHost("10.25.10.104").withPort(6378).withTimeout(Duration.ofSeconds(30)).build());
        uris.add(RedisURI.builder().withHost("10.25.10.104").withPort(6377).withTimeout(Duration.ofSeconds(30)).build());
        uris.add(RedisURI.builder().withHost("10.25.10.104").withPort(6379).withTimeout(Duration.ofSeconds(30)).build());
        LettuceUtil.init(uris);
    }

    private void messageProducer() {
        int loop = 10000;
        System.out.println("send message to ");
        while (true) {
            try{
                loop++;
                if (loop >= 20000) {
                    break;
                }
                RequestEntry requestEntry = new RequestEntry();
                requestEntry.setId(loop).setName("水金吕良").setAge(18).setHobby("可以跑，但是tm没说可以低空飞行啊 ^~^").setCountry("毛里求斯");
                if (loop % 2 == 0) {
                    LettuceUtil.xadd(STREAM_2, requestEntry.convert2Map());
                } else {
                    LettuceUtil.xadd(STREAM_1, requestEntry.convert2Map());
                }
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
            }

        }
        System.out.println("send message to ");

    }


    public static void main(String[] args) {
        TssRedisStreamProducer tssRedisStreamProducer = new TssRedisStreamProducer();
        tssRedisStreamProducer.initRedis();
        tssRedisStreamProducer.messageProducer();

    }

}
